package web.User;

import service.UserService;
import pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    private final UserService service = new UserService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String username= request.getParameter("username");
        String password= request.getParameter("password");

        User user=new User();
        user.setUsersname(username);
        user.setPassword(password);
        User user1=service.selectLogin(user);

        if(user1==null){
            response.sendRedirect("lesson6/login_fail.jsp");
        } else{
            request.getSession().setAttribute("user",user1);
            request.getRequestDispatcher( "index.jsp").forward(request,response);
        }
    }

}
